package com.mio.web.controller.producttype;

import java.util.HashMap;
import java.util.Map;

import org.durcframework.core.GridResult;
import org.durcframework.core.MessageResult;
import org.durcframework.core.controller.CrudController;

import com.mio.web.entity.product.Product;
import com.mio.web.entity.producttype.Producttype;
import com.mio.web.entity.producttype.ProducttypeSch;
import com.mio.web.service.product.ProductService;
import com.mio.web.service.producttype.ProducttypeService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class ProducttypeController extends
		CrudController<Producttype, ProducttypeService> {
	@Autowired
	private ProductService productService;
	
	//新增产品主类
	@RequestMapping("/addMainProducttype.do")
	public @ResponseBody
	MessageResult addMainProducttype(Producttype entity) {
		//格式化创建时间
		if(entity.getCreatedTime().equals("")){
			entity.setCreatedTime(null);
		}
		entity.setParentId(0);
		return this.save(entity);
	}
	
	//新增产品主类
	@RequestMapping("/addAssistantProducttype.do")
	public @ResponseBody
	MessageResult addAssistantProducttype(Producttype entity) {
		//格式化创建时间
		if(entity.getCreatedTime().equals("")){
			entity.setCreatedTime(null);
		}
		return this.save(entity);
	}

	//ɾ����¼
	@RequestMapping("/delProducttype.do")
	public @ResponseBody
	MessageResult delProducttype(Producttype entity) {
		return this.delete(entity);
	}
	
	//�޸ļ�¼
	@RequestMapping("/updateProducttype.do")
	public @ResponseBody
	MessageResult updateProducttype(Producttype entity) {
		return this.update(entity);
	}

	//������ѯ��ҳ����
	@RequestMapping("/listProducttype.do")
	public @ResponseBody
	GridResult listProducttype(ProducttypeSch searchEntitySch) {
		return this.query(searchEntitySch);
	}

	
	//������ѯ���������м�¼
	@RequestMapping("/listAllProducttype.do")
    public @ResponseBody Object listAllProducttype(ProducttypeSch searchEntitySch) {
		if(searchEntitySch.getParentIdSch()!= null){
			searchEntitySch.setSortname("sortId");
		}
        return this.queryAll(searchEntitySch);
    }
	
	//获取所有的产品副类
	@RequestMapping("/listAllAssistantType.do")
    public @ResponseBody Object listAllAssistantType(ProducttypeSch searchEntitySch) {
		return this.getService().getAssistantType(searchEntitySch);
    }

    //��ȡ��ϸ��Ϣ
	@RequestMapping("/getProducttypeById.do")
    public @ResponseBody Producttype getProducttypeById(Producttype entity) {
        return this.getService().get(entity.getId());
    }
	
	
	//根据产品id获取该产品的主类以及副类
	@RequestMapping("/getMainAndAsistantTypeByProductId.do")
	public @ResponseBody Map<String,Object> getMainAndAsistantTypeByProductId(int id){
		Product product = productService.get(id);
		Map<String,Object> map = new HashMap<String, Object>();
		map.put("success", false);
		if(product != null){
			int assistantTypeId = product.getProductTypeId();
			map.put("assistantTypeId", assistantTypeId);
			Producttype assistantType = this.getService().get(assistantTypeId);
			if(assistantType != null){
				int mainTypeId = assistantType.getParentId();
				map.put("mainTypeId", mainTypeId);
				map.put("success", true);
			}
		}
		return map;
	}
	

}